package com.magicode.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.bean.ButtonOperateType;
import com.magicode.cms.entiy.base.Operate;
import com.magicode.cms.entiy.base.Role;
import com.magicode.cms.entiy.base.RolesOperates;
import com.magicode.cms.service.IOperateService;
import com.magicode.common.dao.ICommonDaoService;

@Service("operateService")
public class OperateServiceImpl implements IOperateService {

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Override
	public List<Operate> list() {
		String hql = " from com.magicode.cms.entiy.base.Operate t where t.noPermission=0 ";
		List list = commonDaoService.getListByHQL(hql, null);
		return list;
	}

	@Override
	public List<Operate> list(String whereHql) {
		List list = null;
		if(whereHql != null){
			String hql = " from com.magicode.cms.entiy.base.Operate t "+whereHql;
			list = commonDaoService.getListByHQL(hql, null);
		}
		else{
			list = list();
		}
		return list;
	}
	@Override
	public Map<String, Integer> perSetlist(String roleId) {
		String hql = "select ro from com.magicode.cms.entiy.base.RolesOperates ro left join fetch ro.operate ";
		hql += " where ro.role.id='"+roleId+"' ";
		Map<String, Integer> romap = null;
		try {
			List<RolesOperates> list = this.commonDaoService.getListByHQL(hql);
			romap = new HashMap<String, Integer>();
			for (RolesOperates rolesOperates : list) {
				romap.put(rolesOperates.getOperate().getId(), rolesOperates.getOperateType());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return romap;
	}

	@Override
	public void setOperate(String operateId, String roleId, String type) {
		String hql = "select ro from com.magicode.cms.entiy.base.RolesOperates ro where ro.role.id='"+roleId+"' and ro.operate.id='"+operateId+"' ";
		List<RolesOperates> list = this.commonDaoService.getListByHQL(hql);
		if(list != null && list.size()>0){
			RolesOperates operates = list.get(0);
			operates.setOperateType(Integer.parseInt(type));
			this.commonDaoService.merge(operates);
		}else{
			RolesOperates operates = new RolesOperates();
			Role role = new Role();
			role.setId(roleId);
			Operate operate = new Operate();
			operate.setId(operateId);
			operates.setOperate(operate);
			operates.setRole(role);
			operates.setOperateType(Integer.parseInt(type));
			this.commonDaoService.merge(operates);
		}
	}

	@Override
	public void save(String operateId, String type, String text) {
		Operate operate = (Operate) commonDaoService.get(Operate.class, operateId);
		if("description".equals(type)){
			operate.setDescription(text);
		}
		if("orderCode".equals(type)){
			operate.setOrderCode(text);
		}
		commonDaoService.merge(operate);
	}

	@Override
	public void setButtonOperateType(String operateId, String typeName,
			String type) {
		Operate operate = (Operate) commonDaoService.get(Operate.class, operateId);
		String boType = operate.getButtonOperateType()==null?"":operate.getButtonOperateType();
		Boolean flag = false;
		for (ButtonOperateType emtype : ButtonOperateType.values()) {
			if(boType.contains(typeName) && emtype.toString().equals(typeName)){
				flag = true;
			}
		}
		if(flag){
			if("1".equals(type)){
			}else{
				boType = boType.replaceAll(typeName+",", "");
				operate.setButtonOperateType(boType);
			}
		}else{
			if("1".equals(type)){
				boType += typeName + ",";
				operate.setButtonOperateType(boType);
			}else{
			}
		}
		commonDaoService.merge(operate);
		
	}

}
